<?php

namespace App\Http\Controllers\v1;

use App\Common\Utill\Utill;
use App\Http\Controllers\Controller;

class GcRdsHaController extends Controller
{
    public function Test(){
        return '[{"ClusterName":"10.99.80.58:3336","ClusterAlias":"10010_msxf_riskggroup","ClusterDomain":"risk.msfinance.db","CountInstances":3,"HeuristicLag":0,"HasAutomatedMasterRecovery":true,"HasAutomatedIntermediateMasterRecovery":true},{"ClusterName":"10.99.80.59:3306","ClusterAlias":"10010_msxf_esgroup","ClusterDomain":"esgroup.msfinance.db","CountInstances":3,"HeuristicLag":0,"HasAutomatedMasterRecovery":true,"HasAutomatedIntermediateMasterRecovery":true},{"ClusterName":"10.99.80.59:3316","ClusterAlias":"10011_msxf_vccs","ClusterDomain":"vccs.msfinance.db","CountInstances":3,"HeuristicLag":0,"HasAutomatedMasterRecovery":true,"HasAutomatedIntermediateMasterRecovery":true},{"ClusterName":"10.99.80.59:3326","ClusterAlias":"10011_msxf_pccss","ClusterDomain":"pccs.msfinance.db","CountInstances":3,"HeuristicLag":0,"HasAutomatedMasterRecovery":true,"HasAutomatedIntermediateMasterRecovery":true}]';
    }

    public function test1(){
        return '[{"Key":{"Hostname":"10.99.80.58","Port":3306},"InstanceAlias":"s03cq-idc02s0307u3730-db-mysql-8-10-msxf.host","Uptime":1103420,"ServerID":593366,"ServerUUID":"c05ba43d-f47a-11ed-b7b8-00505696ce81","Version":"5.7.27-log","VersionComment":"Source distribution","FlavorName":"MySQL","ReadOnly":true,"Binlog_format":"ROW","BinlogRowImage":"FULL","LogBinEnabled":true,"LogSlaveUpdatesEnabled":true,"LogReplicationUpdatesEnabled":true,"SelfBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":546,"Type":0},"MasterKey":{"Hostname":"","Port":3306},"MasterUUID":"10cba1b8-c94a-11ed-bdbd-005056960aab","AncestryUUID":"10cba1b8-c94a-11ed-bdbd-005056960aab,c05ba43d-f47a-11ed-b7b8-00505696ce81","IsDetachedMaster":false,"Slave_SQL_Running":true,"ReplicationSQLThreadRuning":true,"Slave_IO_Running":true,"ReplicationIOThreadRuning":true,"ReplicationSQLThreadState":1,"ReplicationIOThreadState":1,"HasReplicationFilters":false,"GTIDMode":"ON","SupportsOracleGTID":true,"UsingOracleGTID":false,"UsingMariaDBGTID":false,"UsingPseudoGTID":false,"ReadBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":546,"Type":0},"ExecBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":546,"Type":0},"IsDetached":false,"RelaylogCoordinates":{"LogFile":"slave-relay-bin.000002","LogPos":712,"Type":1},"LastSQLError":"","LastIOError":"","SecondsBehindMaster":{"Int64":0,"Valid":true},"SQLDelay":0,"ExecutedGtidSet":"10cba1b8-c94a-11ed-bdbd-005056960aab:1-2","GtidPurged":"","GtidErrant":"","SlaveLagSeconds":{"Int64":0,"Valid":true},"ReplicationLagSeconds":{"Int64":0,"Valid":true},"SlaveHosts":[],"Replicas":[],"ClusterName":"10.99.80.59:3306","SuggestedClusterAlias":"10010_msxf_esgroup","DataCenter":"s03cq","Region":"","PhysicalEnvironment":"","ReplicationDepth":1,"IsCoMaster":false,"HasReplicationCredentials":true,"ReplicationCredentialsAvailable":true,"SemiSyncAvailable":false,"SemiSyncPriority":0,"SemiSyncMasterEnabled":false,"SemiSyncReplicaEnabled":false,"SemiSyncMasterTimeout":0,"SemiSyncMasterWaitForReplicaCount":0,"SemiSyncMasterStatus":false,"SemiSyncMasterClients":0,"SemiSyncReplicaStatus":false,"LastSeenTimestamp":"2023-09-06 10:00:57","IsLastCheckValid":true,"IsUpToDate":true,"IsRecentlyChecked":true,"SecondsSinceLastSeen":{"Int64":2,"Valid":true},"CountMySQLSnapshots":0,"IsCandidate":false,"PromotionRule":"neutral","IsDowntimed":false,"DowntimeReason":"","DowntimeOwner":"","DowntimeEndTimestamp":"","ElapsedDowntime":0,"UnresolvedHostname":"","AllowTLS":false,"Problems":[],"LastDiscoveryLatency":8773444,"ReplicationGroupName":"","ReplicationGroupIsSinglePrimary":false,"ReplicationGroupMemberState":"","ReplicationGroupMemberRole":"","ReplicationGroupMembers":[],"ReplicationGroupPrimaryInstanceKey":{"Hostname":"","Port":0}},{"Key":{"Hostname":"10.99.80.59","Port":3306},"InstanceAlias":"s03cq-idc02s0307u3731-db-mysql-8-11-msxf.host","Uptime":1023509,"ServerID":593306,"ServerUUID":"10cba1b8-c94a-11ed-bdbd-005056960aab","Version":"5.7.27-log","VersionComment":"Source distribution","FlavorName":"MySQL","ReadOnly":false,"Binlog_format":"ROW","BinlogRowImage":"FULL","LogBinEnabled":true,"LogSlaveUpdatesEnabled":true,"LogReplicationUpdatesEnabled":true,"SelfBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":546,"Type":0},"MasterKey":{"Hostname":"1","Port":0},"MasterUUID":"","AncestryUUID":"10cba1b8-c94a-11ed-bdbd-005056960aab","IsDetachedMaster":false,"Slave_SQL_Running":false,"ReplicationSQLThreadRuning":false,"Slave_IO_Running":false,"ReplicationIOThreadRuning":false,"ReplicationSQLThreadState":-1,"ReplicationIOThreadState":-1,"HasReplicationFilters":false,"GTIDMode":"ON","SupportsOracleGTID":true,"UsingOracleGTID":false,"UsingMariaDBGTID":false,"UsingPseudoGTID":false,"ReadBinlogCoordinates":{"LogFile":"","LogPos":0,"Type":0},"ExecBinlogCoordinates":{"LogFile":"","LogPos":0,"Type":0},"IsDetached":false,"RelaylogCoordinates":{"LogFile":"","LogPos":0,"Type":1},"LastSQLError":"","LastIOError":"","SecondsBehindMaster":{"Int64":0,"Valid":false},"SQLDelay":0,"ExecutedGtidSet":"10cba1b8-c94a-11ed-bdbd-005056960aab:1-2","GtidPurged":"","GtidErrant":"","SlaveLagSeconds":{"Int64":0,"Valid":false},"ReplicationLagSeconds":{"Int64":0,"Valid":false},"SlaveHosts":[{"Hostname":"10.99.80.58","Port":3306},{"Hostname":"10.99.80.60","Port":3306}],"Replicas":[{"Hostname":"10.99.80.58","Port":3306},{"Hostname":"10.99.80.60","Port":3306}],"ClusterName":"10.99.80.59:3306","SuggestedClusterAlias":"10010_msxf_esgroup","DataCenter":"s03cq","Region":"","PhysicalEnvironment":"","ReplicationDepth":0,"IsCoMaster":false,"HasReplicationCredentials":false,"ReplicationCredentialsAvailable":true,"SemiSyncAvailable":false,"SemiSyncPriority":0,"SemiSyncMasterEnabled":false,"SemiSyncReplicaEnabled":false,"SemiSyncMasterTimeout":0,"SemiSyncMasterWaitForReplicaCount":0,"SemiSyncMasterStatus":false,"SemiSyncMasterClients":0,"SemiSyncReplicaStatus":false,"LastSeenTimestamp":"2023-09-06 10:00:57","IsLastCheckValid":true,"IsUpToDate":true,"IsRecentlyChecked":true,"SecondsSinceLastSeen":{"Int64":2,"Valid":true},"CountMySQLSnapshots":0,"IsCandidate":false,"PromotionRule":"neutral","IsDowntimed":false,"DowntimeReason":"","DowntimeOwner":"","DowntimeEndTimestamp":"","ElapsedDowntime":0,"UnresolvedHostname":"","AllowTLS":false,"Problems":[],"LastDiscoveryLatency":8926523,"ReplicationGroupName":"","ReplicationGroupIsSinglePrimary":false,"ReplicationGroupMemberState":"","ReplicationGroupMemberRole":"","ReplicationGroupMembers":[],"ReplicationGroupPrimaryInstanceKey":{"Hostname":"","Port":0}},{"Key":{"Hostname":"10.99.80.60","Port":3306},"InstanceAlias":"scqbn-idc02s0307u3732-db-mysql-8-12-msxf.host","Uptime":1109881,"ServerID":593316,"ServerUUID":"d2301b32-f477-11ed-a855-00505696c837","Version":"5.7.27-log","VersionComment":"Source distribution","FlavorName":"MySQL","ReadOnly":true,"Binlog_format":"ROW","BinlogRowImage":"FULL","LogBinEnabled":true,"LogSlaveUpdatesEnabled":true,"LogReplicationUpdatesEnabled":true,"SelfBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":546,"Type":0},"MasterKey":{"Hostname":"10.99.80.59","Port":3306},"MasterUUID":"10cba1b8-c94a-11ed-bdbd-005056960aab","AncestryUUID":"10cba1b8-c94a-11ed-bdbd-005056960aab,d2301b32-f477-11ed-a855-00505696c837","IsDetachedMaster":false,"Slave_SQL_Running":true,"ReplicationSQLThreadRuning":true,"Slave_IO_Running":true,"ReplicationIOThreadRuning":true,"ReplicationSQLThreadState":1,"ReplicationIOThreadState":1,"HasReplicationFilters":false,"GTIDMode":"ON","SupportsOracleGTID":true,"UsingOracleGTID":true,"UsingMariaDBGTID":false,"UsingPseudoGTID":false,"ReadBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":546,"Type":0},"ExecBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":546,"Type":0},"IsDetached":false,"RelaylogCoordinates":{"LogFile":"slave-relay-bin.000002","LogPos":759,"Type":1},"LastSQLError":"","LastIOError":"","SecondsBehindMaster":{"Int64":0,"Valid":true},"SQLDelay":0,"ExecutedGtidSet":"10cba1b8-c94a-11ed-bdbd-005056960aab:1-2","GtidPurged":"","GtidErrant":"","SlaveLagSeconds":{"Int64":0,"Valid":true},"ReplicationLagSeconds":{"Int64":0,"Valid":true},"SlaveHosts":[],"Replicas":[],"ClusterName":"10.99.80.59:3306","SuggestedClusterAlias":"10010_msxf_esgroup","DataCenter":"scqbn","Region":"","PhysicalEnvironment":"","ReplicationDepth":1,"IsCoMaster":false,"HasReplicationCredentials":true,"ReplicationCredentialsAvailable":true,"SemiSyncAvailable":false,"SemiSyncPriority":0,"SemiSyncMasterEnabled":false,"SemiSyncReplicaEnabled":false,"SemiSyncMasterTimeout":0,"SemiSyncMasterWaitForReplicaCount":0,"SemiSyncMasterStatus":false,"SemiSyncMasterClients":0,"SemiSyncReplicaStatus":false,"LastSeenTimestamp":"2023-09-06 10:00:57","IsLastCheckValid":true,"IsUpToDate":true,"IsRecentlyChecked":true,"SecondsSinceLastSeen":{"Int64":2,"Valid":true},"CountMySQLSnapshots":0,"IsCandidate":false,"PromotionRule":"neutral","IsDowntimed":false,"DowntimeReason":"","DowntimeOwner":"","DowntimeEndTimestamp":"","ElapsedDowntime":0,"UnresolvedHostname":"","AllowTLS":false,"Problems":[],"LastDiscoveryLatency":2916829,"ReplicationGroupName":"","ReplicationGroupIsSinglePrimary":false,"ReplicationGroupMemberState":"","ReplicationGroupMemberRole":"","ReplicationGroupMembers":[],"ReplicationGroupPrimaryInstanceKey":{"Hostname":"","Port":0}}]';
    }

    public function test2 ($masterHost, $masterPort, $slaveHost, $slavePort){
        var_dump($masterHost, $masterPort, $slaveHost, $slavePort);
        return '';
    }
    public function test3 (){
        return '{"RaftBind":"10.246.2.211:10010","RaftAdvertise":"10.246.2.211:10010","State":"Follower","Healthy":true,"IsPartOfQuorum":true,"Leader":"127.0.0.1:10010","LeaderURI":"http://127.0.0.1:3000","Peers":["127.0.0.1:10010","10.246.2.204:10010","10.246.2.211:10010"]}';
    }
    public function topology ($host, $port){
        return '{"Code":"OK","Message":"Topology for cluster 10.246.2.204:3306","Details":"10.246.2.204:3306 (ecs-hostname-20230921049)   [0s,ok,5.7.27-log,rw,ROW,\u003e\u003e,GTID]\n- 10.246.2.211:3306 (ECS-hostname-20230921049) [null,nonreplicating,5.7.27-log,ro,ROW,\u003e\u003e,GTID]\n+ 127.0.0.1:3306 (ECS-hostname-20230921049)  [0s,ok,5.7.27-log,rw,ROW,\u003e\u003e,GTID]"}';
    }
    public function discover (){
        sleep(10);
        return '{"Code":"OK","Message":"Instance discovered: 10.246.2.211:3306","Details":{"Key":{"Hostname":"10.246.2.211","Port":3306},"InstanceAlias":"ECS-hostname-20230921049","Uptime":1724792,"ServerID":593366,"ServerUUID":"470e76c0-585f-11ee-9902-fa163e730180","Version":"5.7.27-log","VersionComment":"Source distribution","FlavorName":"","ReadOnly":true,"Binlog_format":"ROW","BinlogRowImage":"FULL","LogBinEnabled":true,"LogSlaveUpdatesEnabled":true,"LogReplicationUpdatesEnabled":true,"SelfBinlogCoordinates":{"LogFile":"mysql-bin.000001","LogPos":1939,"Type":0},"MasterKey":{"Hostname":"10.246.2.204","Port":3306},"MasterUUID":"77c4d56f-585f-11ee-b86b-fa163e18c65d","AncestryUUID":"77c4d56f-585f-11ee-b86b-fa163e18c65d,470e76c0-585f-11ee-9902-fa163e730180","IsDetachedMaster":false,"Slave_SQL_Running":false,"ReplicationSQLThreadRuning":false,"Slave_IO_Running":false,"ReplicationIOThreadRuning":true,"ReplicationSQLThreadState":0,"ReplicationIOThreadState":0,"HasReplicationFilters":false,"GTIDMode":"ON","SupportsOracleGTID":true,"UsingOracleGTID":true,"UsingMariaDBGTID":false,"UsingPseudoGTID":false,"ReadBinlogCoordinates":{"LogFile":"mysql-bin.000002","LogPos":234,"Type":0},"ExecBinlogCoordinates":{"LogFile":"mysql-bin.000002","LogPos":234,"Type":0},"IsDetached":false,"RelaylogCoordinates":{"LogFile":"slave-relay-bin.000009","LogPos":367,"Type":1},"LastSQLError":"","LastIOError":"","SecondsBehindMaster":{"Int64":0,"Valid":false},"SQLDelay":0,"ExecutedGtidSet":"470e76c0-585f-11ee-9902-fa163e730180:1-6,\n77c4d56f-585f-11ee-b86b-fa163e18c65d:1","GtidPurged":"","GtidErrant":"","SlaveLagSeconds":{"Int64":0,"Valid":false},"ReplicationLagSeconds":{"Int64":0,"Valid":false},"SlaveHosts":[],"Replicas":[],"ClusterName":"10.246.2.204:3306","SuggestedClusterAlias":"10010_msxf_riskggroup","DataCenter":"ECS","Region":"","PhysicalEnvironment":"","ReplicationDepth":1,"IsCoMaster":false,"HasReplicationCredentials":true,"ReplicationCredentialsAvailable":true,"SemiSyncAvailable":false,"SemiSyncPriority":0,"SemiSyncMasterEnabled":false,"SemiSyncReplicaEnabled":false,"SemiSyncMasterTimeout":0,"SemiSyncMasterWaitForReplicaCount":0,"SemiSyncMasterStatus":false,"SemiSyncMasterClients":0,"SemiSyncReplicaStatus":false,"LastSeenTimestamp":"","IsLastCheckValid":true,"IsUpToDate":true,"IsRecentlyChecked":true,"SecondsSinceLastSeen":{"Int64":0,"Valid":false},"CountMySQLSnapshots":0,"IsCandidate":false,"PromotionRule":"neutral","IsDowntimed":false,"DowntimeReason":"","DowntimeOwner":"","DowntimeEndTimestamp":"","ElapsedDowntime":0,"UnresolvedHostname":"","AllowTLS":false,"Problems":[],"LastDiscoveryLatency":2071152,"ReplicationGroupName":"","ReplicationGroupIsSinglePrimary":false,"ReplicationGroupMemberState":"","ReplicationGroupMemberRole":"","ReplicationGroupMembers":[],"ReplicationGroupPrimaryInstanceKey":{"Hostname":"","Port":0}}}';
    }

    public function vip()
    {
        $r = [
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'id' => '172.17.45.180',
                'vipAddress' => '172.17.45.180',
                'requestId' => '172.17.45.180',
            ]
        ];
        return json_encode($r);
    }
    public function vips()
    {
        $r = [
            'code' => 0,
            'msg' => 'success',
            'data' => [
                [
                    'id' => '172.17.45.180',
                ],
                [
                    'id' => '172.17.45.180',
                ]
            ]
        ];
        return json_encode($r);
    }
    public function vipDelete($id)
    {
        if ($id < 0 ) {
            return $this->error();
        }

        $r = [
            'code' => 0,
            'msg' => 'success',
            'requestId' => '172.17.45.180'
        ];
        return json_encode($r);
    }
    public function vipBind()
    {
        $r = [
            'code' => 0,
            'msg' => 'success',
            'requestId' => '172.17.45.180'
        ];
        return json_encode($r);
    }

    public function createHa()
    {
        $r = Utill::httpRequest("http://localhost:8081/v1/getRDSInfos","POST");
        return $r;
    }
}